/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class BounceMsg
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  60 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  61 */     Account acct = getRequestedAccount(zsc);
/*  62 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  63 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  65 */     Element msgElem = request.getElement("m");
/*  66 */     ItemId iid = new ItemId(msgElem.getAttribute("id"), zsc);
/*     */     
/*  68 */     MailSender msender = mbox.getMailSender().setSaveToSent(false).setRedirectMode(true).setSkipHeaderUpdate(true);
/*     */     
/*  70 */     FileUploadServlet.Upload upload = null;
/*     */     try { InputStream is;
/*     */       InputStream is;
/*  73 */       if (iid.belongsTo(mbox)) {
/*  74 */         is = mbox.getMessageById(octxt, iid.getId()).getContentStream(); } else { InputStream is;
/*  75 */         if (iid.isLocal()) {
/*  76 */           Mailbox mboxSrc = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
/*  77 */           is = mboxSrc.getMessageById(octxt, iid.getId()).getContentStream();
/*     */         } else {
/*  79 */           upload = UserServlet.getRemoteResourceAsUpload(zsc.getAuthToken(), iid, Maps.newHashMap());
/*  80 */           is = upload.getInputStream();
/*     */         }
/*     */       }
/*  83 */       ZMimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(acct), is);
/*  84 */       addResentHeaders(msgElem, mm, zsc, octxt, acct, msender);
/*  85 */       msender.sendMimeMessage(octxt, mbox, mm);
/*     */     } catch (MessagingException me) {
/*  87 */       throw ServiceException.FAILURE("error generating new message", me);
/*     */     } catch (IOException ioe) {
/*  89 */       throw ServiceException.FAILURE("error fetching remote message", ioe);
/*     */     }
/*     */     finally {
/*  92 */       if (upload != null) {
/*  93 */         FileUploadServlet.deleteUpload(upload);
/*     */       }
/*     */     }
/*     */     
/*  97 */     Element response = zsc.createElement(MailConstants.BOUNCE_MSG_RESPONSE);
/*  98 */     return response;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   MailSender addResentHeaders(Element msgElem, ZMimeMessage mm, ZimbraSoapContext zsc, OperationContext octxt, Account acct, MailSender msender)
/*     */     throws MessagingException, ServiceException
/*     */   {
/* 111 */     ParseMimeMessage.MessageAddresses maddrs = getResentAddressees(msgElem, acct, msender);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 120 */     String msgid = new ZMimeMessage(mm.getSession()).getMessageID();
/* 121 */     mm.addHeader("Resent-Message-ID", msgid);
/*     */     
/* 123 */     List<String> recipients = new ArrayList(5);
/* 124 */     recipients.addAll(addResentRecipientHeader(mm, "Resent-Bcc", maddrs.get(ToXML.EmailType.BCC.toString())));
/* 125 */     recipients.addAll(addResentRecipientHeader(mm, "Resent-Cc", maddrs.get(ToXML.EmailType.CC.toString())));
/* 126 */     recipients.addAll(addResentRecipientHeader(mm, "Resent-To", maddrs.get(ToXML.EmailType.TO.toString())));
/* 127 */     if (recipients.isEmpty()) {
/* 128 */       throw ServiceException.INVALID_REQUEST("no recipients specified", null);
/*     */     }
/* 130 */     InternetAddress rfrom = (InternetAddress)ArrayUtil.getFirstElement(maddrs.get(ToXML.EmailType.FROM.toString()));
/* 131 */     InternetAddress rsender = (InternetAddress)ArrayUtil.getFirstElement(maddrs.get(ToXML.EmailType.SENDER.toString()));
/* 132 */     Pair<InternetAddress, InternetAddress> fromsender = msender.getSenderHeaders(rfrom, rsender, acct, getAuthenticatedAccount(zsc), octxt != null ? octxt.isUsingAdminPrivileges() : false);
/*     */     
/* 134 */     InternetAddress from = (InternetAddress)fromsender.getFirst();
/* 135 */     InternetAddress sender = (InternetAddress)fromsender.getSecond();
/* 136 */     assert (from != null);
/* 137 */     if (sender != null) {
/* 138 */       mm.addHeader("Resent-Sender", sender.toString());
/*     */     }
/* 140 */     mm.addHeader("Resent-From", from.toString());
/*     */     
/* 142 */     mm.addHeader("Resent-Date", DateUtil.toRFC822Date(new Date(System.currentTimeMillis())));
/*     */     
/* 144 */     mm.saveChanges();
/*     */     
/*     */ 
/* 147 */     msender.setEnvelopeFrom(from.getAddress());
/* 148 */     msender.setRecipients((String[])recipients.toArray(new String[recipients.size()]));
/* 149 */     return msender;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   ParseMimeMessage.MessageAddresses getResentAddressees(Element msgElem, Account acct, MailSender msender)
/*     */     throws ServiceException
/*     */   {
/* 163 */     String defaultCharset = acct.getPrefMailDefaultCharset();
/* 164 */     if (Strings.isNullOrEmpty(defaultCharset)) {
/* 165 */       defaultCharset = "utf-8";
/*     */     }
/*     */     
/* 168 */     ParseMimeMessage.MessageAddresses maddrs = new ParseMimeMessage.MessageAddresses();
/* 169 */     for (Element e : msgElem.listElements("e")) {
/*     */       try {
/* 171 */         maddrs.add(e, defaultCharset);
/*     */       } catch (IOException ioe) {
/* 173 */         throw ServiceException.FAILURE("error generating addressees", ioe);
/*     */       }
/*     */     }
/* 176 */     if (maddrs.isEmpty()) {
/* 177 */       throw ServiceException.INVALID_REQUEST("no recipients specified", null);
/*     */     }
/* 179 */     return maddrs;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   List<String> addResentRecipientHeader(MimeMessage mm, String name, InternetAddress[] addrs)
/*     */     throws MessagingException
/*     */   {
/* 193 */     if ((addrs == null) || (addrs.length == 0)) {
/* 194 */       return Collections.emptyList();
/*     */     }
/* 196 */     mm.addHeader(name, Joiner.on(", ").join(addrs));
/*     */     
/* 198 */     List<String> recipients = new ArrayList(5);
/* 199 */     for (InternetAddress addr : addrs) {
/* 200 */       recipients.add(addr.getAddress());
/*     */     }
/* 202 */     return recipients;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/BounceMsg.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */